using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using System.Threading;
using System.Text.RegularExpressions;

namespace TripTracker
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
            
            //cellIdThread = new Thread(new ThreadStart(getCellId));
            //cellIdThread.Start();
            
            //Thread ciao = new Thread(new ThreadStart(prova));
            //ciao.Start();
        }

        //private void getCellId() {
        //    int i = 0;
        //    while (i < 2)
        //    {
        //        GSMModemConnector.SendModemCommand("COM9:", "AT+CREG=2\r", "AT+CREG?\r");
        //        Thread.Sleep(2000);
        //        i++;
        //    }
        //}

        // private void prova() {
        //    int i = 0;
        //    while (i < 4)
        //    {
        //        Thread.Sleep(1000);
        //        MessageBox.Show(GSMModemConnector.cellId);
        //        i++;
        //    }
        //}

        /// <summary>
        /// Displays the settings form if the tracking system is not running
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void settings_Click(object sender, EventArgs e)
        {
            if ( GPSConnection.getGPSConnection().connected ) {
                MessageBox.Show("Cannot change settings while tracking!");
            } else {
                SettingsForm settings = new SettingsForm();
                settings.Visible = true;
            }
        }

        /// <summary>
        /// Starts the tracking functionality by hangling the start-button click
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void start_Click(object sender, EventArgs e)
        {

            if (! GPSConnection.getGPSConnection().connected )
            {
                try
                {   
                    if(Settings.getSettings().WebTrackerActivated)
                    {   
                        try {
                            TripTrackerWebService.TripTrackerWebService TripTrackerWebService = new TripTracker.TripTrackerWebService.TripTrackerWebService();
                            if(!TripTrackerWebService.checkUser(Settings.getSettings().WebTrackerUserName, Settings.getSettings().WebTrackerPassword))
                            {
                                MessageBox.Show("Warning! Wrong username or password!");
                                return;
                            }
                        } catch
                        {
                            MessageBox.Show("Warning! No internet connection available!");
                        }

                    } 
                    if (GPSConnection.getGPSConnection().connect())
                    {
                        menuItem2.Text = "Stop";
                        pictureBoxNotConnected.Visible = false;
                    }
                }
                catch
                {
                    MessageBox.Show("Warning! Cannot open port " + Settings.getSettings().GPSPort + ".");
                    menuItem2.Text = "Start";
                    pictureBoxNotConnected.Visible = true;
                }
            }
            else
            {
                GPSConnection.getGPSConnection().disconnect();
                menuItem2.Text = "Start";
                pictureBoxNotConnected.Visible = true;
            }

        }
        
        /// <summary>
        /// Closes the application
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void close_Click(object sender, EventArgs e)
        {
            if (GPSConnection.getGPSConnection().connected)
            {
                MessageBox.Show("Cannot change settings while tracking!");
            }
            else
            {
                Close();
            }
        }

        /// <summary>
        /// Handles the export-button click to export the collected coordinates to a kml file
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void export_Click(object sender, EventArgs e)
        {
            if (GPSConnection.getGPSConnection().connected)
            {
                MessageBox.Show("Cannot export while tracking!");
            }
            else
            {
                GPSExport ge = new GPSExport();
                ge.gpsExport();
            }
        }

        private void menuItem6_Click(object sender, EventArgs e)
        {
            MessageBox.Show( "CellID: " + GSMModemConnector.getCellID() + "\n LAI: " + GSMModemConnector.getLAI());
        }

    }
}